﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;
using Alfred.Ressource;

namespace Alfred
{
	class Playlist
	{
		public string pathMusic { get; set; }
		public string pathPlaylist { get; set; }
		public int sizePlaylist { get; set; }
		public List<string> audioExtensions = new List<string>(new string[] { ".mp3", ".mp2", ".wma", ".wav" });
        public List<string> genreList { get; set; }
        public List<string> artistList { get; set; }

		public Playlist()
		{
			deletePlaylist();
			
			DirectoryInfo playlistDir = new DirectoryInfo(Paths.path_playlist);

			if (!playlistDir.Exists)
				playlistDir.Create();

			pathPlaylist = playlistDir.FullName;
			pathMusic = Paths.path_music;
			sizePlaylist = 10;
            fillArtistsAndGenresList();
		}

		public void deletePlaylist()
		{
			DirectoryInfo playlistDir = new DirectoryInfo(Paths.path_playlist);
			if (playlistDir.Exists)
				playlistDir.Delete(true);
			return;
		}

		public void createPlaylist(List<string> liste, string nom)
		{
            Shuffle(liste);
            liste = liste.GetRange(0, Math.Min(10, liste.Count));
			string fullName = pathPlaylist + '/' + nom + ".m3u";

			FileInfo playlist = new FileInfo(fullName);
			if (!playlist.Exists)
			{
				FileStream fs = playlist.Create();
				fs.Dispose();
			}
			
			StreamWriter writer = new StreamWriter(fullName);
			writer.WriteLine("#EXTM3U");

			foreach (string path in liste)
			{
				string[] tab = path.Split('\\', '.');
				writer.WriteLine(path);
			}
			writer.Close(); 
		}

		public List<string> createFileList(string folderName)
		{
			DirectoryInfo genreFolder = new DirectoryInfo(folderName);
			if (!genreFolder.Exists)
				return new List<string>();

			Random rand = new Random();

			FileInfo[] files = genreFolder.GetFiles("*.*", SearchOption.AllDirectories);
            return files.Where(f => audioExtensions.Exists(a => a == f.Extension)).Select(f => f.FullName).ToList();
		}

        public void Shuffle(List<string> list)
        {
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                string value = list[k];
                list[k] = list[n];
                list[n] = value;
            }  
        }

		public void createPlaylistByGenre(string genre)
		{
			List<string> listFile = createFileList(pathMusic + '/' + genre);
			createPlaylist(listFile, genre);
		}

		public void createPlaylistByArtist(string artist)
		{
			DirectoryInfo musicFolder = new DirectoryInfo(pathMusic);

			if (!musicFolder.Exists)
				return;

			List<string> listFile = new List<string>();

			DirectoryInfo[] artistFolders = musicFolder.GetDirectories(artist, SearchOption.AllDirectories);

			foreach (DirectoryInfo artistFolder in artistFolders)
			{
				listFile.AddRange(createFileList(artistFolder.FullName));
			}

			createPlaylist(listFile, artist);
		}

		public void launchPlaylist(string nom)
		{
			Process wmp = new Process();
			Process.Start(Paths.path_wmp, "\"" + pathPlaylist + '/' + nom + ".m3u" + "\"");
		}

		public void fillArtistsAndGenresList()
		{
            genreList = new List<string>();
            artistList = new List<string>();
			DirectoryInfo musicFolder = new DirectoryInfo(pathMusic);
			if (!musicFolder.Exists)
				return;
			
			DirectoryInfo[] genreFolders = musicFolder.GetDirectories();

			foreach (DirectoryInfo genreFolder in genreFolders)
			{
				genreList.Add(genreFolder.Name);
                DirectoryInfo[] artistFolders = genreFolder.GetDirectories();
                foreach (DirectoryInfo artistFolder in artistFolders)
                    artistList.Add(artistFolder.Name);
			}
		}
	}
}
